package com.example.uitl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneNumEncrypt {

	public static void main(String[] args) {
		
//		String phone ="4f704d56ff5e42ddb8e72b39d7b0254b";
//		phone= formatStr(phone); 
//		String ret = getEncryptStr(phone);
//		String sb = phone;
//		if(!ret.equals(phone)){
//			sb = PhoneNumEncrypt.decrypt(ret);
//		}
//		if (ret.length() > 20 || !phone.equals(sb)) {
//			System.out.println(ret + "   ||   "+ sb + " || " + ret.length() + " || " + phone.equals(sb) + " || " + phone+" || " + phone.length());
//		}

//		FileOutputStream o = null;
//		try {
//			o = new FileOutputStream("d://phone22.txt");
//			List<String> plist = addphone();
//			for (String phone : plist) {
//				phone = phone.replaceAll("[^-\\d]", "");
//				o.write((phone + "||" + isMobile(phone) +""+"\n").getBytes("GBK"));
//			}
//			o.close();
//		} catch (Exception e) {
//		} finally {
//			if (o != null) {
//				try {
//					o.close();
//				} catch (Exception e) {
//				}
//			}
//		}
//		System.out.println("13632885389^".replaceAll("[^-\\d]", ""));
		
//		List<String> plist = addphone();
//		for (String phone : plist) {
//			String tel = "";
//			
//			if(phone == null || phone.length() < 4 || judgeContainsStr(phone)){
//				tel = phone;
//			} else {
//				tel= phone.replace("\\", "、"); 
//				tel= tel.replaceAll("[^-、\\d]", ""); 
//			}
//			
//			String ret = getEncryptStr(phone);
//			
//			String sb = PhoneNumEncrypt.decrypt(ret);
//			
//			if (!tel.equals(sb)) {
//				System.out.println(ret + "   ||   "+ sb + " || " + tel.equals(sb) + " || " +  " || " + phone);
//			}
//		}

		List<String> list = java.util.Arrays.asList("81453760","25623822","88308296","28537807","k97kw4bckgga023","28522892","33297979","89755388","28083992","88312563","k6k8wb-q8gowg448","82443424","25560958","33823012","w17c7kqb085","84649918,61231119","0","26525404","0","89367733","28500522","0","k5k1wbq1qcpc213","0","89483045","61254591","33919876","0","28121523","82472700","89583932,25331006","83698854","0","0","0","86182757","89598011","82736652","0","83593353","33501769","84889117","h64a0paohqg956","61891829","61891829","k6k1wb-wq0hkk488","0","28305959","0","0","k437kwb-qgqoo287","h10qkcq2827","82226556","g1p62wcb856","0","k16k1wb-cohcg659","29018797","h7p6gga3134","0","0","0","22951105","0","89637968","0","0","0","28235684","0","0","k7kw1b-q7oooh823","k685kwb-cgbbp552","0","k578kwb-cphqk568","k41kw4b-cghqc527","0","27759229","86314912","29554780","0","29034200","86705109","k9k9wb-c3phgp233","28776372","0","33554033","28212777","0","0","0","0","k89kwb2qobwo771","28138179","0","p159bqqh067","89649985","26978832","61562232","0","0","0","0755-89603839","0","q7b4o5qp411","0","0","27925863","28957382","0","0","0","82983839","k85kw8b-cpqqc753","0","0","g6p1wcb3022","29111907","0","0","0","82025155","0","0","0","0","0","25729583","0","0","0","0","0","0","0","0","0","0","0","0","0","32869635","0","0","0","28443020","0","0","0","0","0","k67kwb-cqgw9g646","w2kh45qa739","0","0","0","0","k39kw3b-cqabp999","0","0","kkwb1-qo19kcp262","kkwb1-qo19kcp262","0","w87agqh2530","26165581","0","0","b3b6gcp5553","23908816","23908816","23908816","0","0","0","k08kwbgc1pcb712","0","0","0","g16bgqo5768","0","0","0","k661kwb-ckwpo400","0","k696kwbcgkbg193","0","25875052","22675450","0","0","-01acbobpg7cghk-q303","g83gpqw1018","0","0","0","0","0","0","k309kkqw533","36636622","w131pcgg896","o522oqqh143","83200029","0","0","#N/A","p65cc5qa295","k164kwb-qgagb188","0","a8p0w2cb163","k24kwb-qg1cgc879","k8kwb81-qogao515","0","q1pgqa00100","o0ppc14b979","0","0","k541kwb-cgbac857","0","0","k4k13wb-qowap053","0","q02hpqw6795","89703696","0","0","0","0","k2k6wb-cq5hqc273","86133130","0","q71k9ggc528","0","0","0","0","k48kw6bcwaaa159","0","0","wk46hqa1117","0","0","0","a162qwcb387","0","0","075527888834","075527888834","0","0","k47kwbcggbq1057","kkwb-gog184bp360","b0gq3qc7681","w95ohq7p992","kkw39b-c1abbh654","0","0","0","26990000","0","kk8w5bggb5wb961","kk8w5bggb5wb961","0","k11kwb-2qawgp080","k041kwbcqgbc346","0","hk5aow19531","0","bo3oqc55134","0","23087086","g471aqca750","g471aqca750","0","q911aggq989","0","k0kwbgg1ao2c079","0","0","k73k9wbcgpag916","0","0","k93kwb-6qgqck472","w019qoo452","0","0","0","kw147bbkqkcqa959","0","0","23462525","27589683","0","k41k6wb-copbg433","0","0","o54ckqg1583","0","a12boqa0098","k698kwb-qqobq617","0","0","w4gq1cc2966","0","0","0","28617160","kk1wb-c8co1wb560","0","p10owbqochb4808","0","0");

		for (String p : list) {
			System.out.println(PhoneNumEncrypt.decrypt(p));
		}
	}

	//加密手机号码
	private static String getEncryptStr(String tel) {

		if(tel == null || tel.length() < 4 || judgeContainsStr(tel)){
			return tel;
		}

		StringBuffer encryptTel = new StringBuffer();
		String top = "";
		String min = "";
		String after = "";
		String encryptStr = "";
		try{
			
			tel= tel.replace("\\", "、"); 
			tel= tel.replaceAll("[^-、\\d]", ""); 
			
			if(isMobile(tel)){   //手机号码 留前三  后三 中间加密
				top = tel.substring(0, 3);
				min = tel.substring(3, 8);
				encryptStr = JMArith.encrypt(min);
				after = tel.substring(8, 11);
				encryptTel.append(top).append(encryptStr).append(after);
			}else{  //其他形式   留后三   其他全加密
//				tel = formatStr(tel);
				min = tel.substring(0, tel.length()-3);
				encryptStr = JMArith.encrypt(min);
				after = tel.substring(tel.length()-3, tel.length());
				encryptTel.append(top).append(encryptStr).append(after);
			}
		}catch(Exception e){
			return tel;
		}
		return encryptTel.toString();
	}
	
	//解密 电话号码
	public static String decrypt(String tel){
		String top = "";
		String min = "";
		String after = "";
		String decryptStr = "";
		
		if(tel == null || tel.length() <10){
			return tel;
		}
		StringBuffer sb = new StringBuffer();
		int index = tel.indexOf("-");
		try{
			if(index == -1 && tel.length() == 14 && tel.startsWith("1")){  //手机号码
				top = tel.substring(0, 3);
				min = tel.substring(3,tel.length()-3);
				after = tel.substring(tel.length()-3,tel.length());
				decryptStr = JMArith.decrypt(min);
				sb.append(top).append(decryptStr).append(after);
			}else{   //其他任何情况
				top = tel.substring(0, tel.length()-3);
				after = tel.substring(tel.length()-3,tel.length());
				decryptStr = JMArith.decrypt(top);
				sb.append(decryptStr).append(after);
			}
		}catch(Exception e){
			return tel;
		}
		return sb.toString();
	}
	
	//格式化 电话号码
	private static String formatStr(String tel) {
		tel=tel.replaceAll(" ", "");
		tel=tel.replaceAll("－", "-");
		tel=tel.replaceAll("--", "-");
		tel=tel.replaceAll("—", "-");
		tel=tel.replaceAll("转", "-");
		return tel;

	}
	private static boolean isCharacter(String tel) {
		
		return tel.matches(".*[a-zA-z].*");

	}
	
	/**
	 * 判断字符串是否包含字母
	 * @param str
	 * @return
	 */
	public static boolean judgeContainsStr(String str) {
		String regex = ".*[a-zA-Z]+.*";
		Matcher m = Pattern.compile(regex).matcher(str);
		return m.matches();
	}
	
	/**
	电信号段:133/
	   153/
	   177/
	   180/181/189/；
	联通号段:130/131/132/
		145/
		155/156/
		176/；
		185/186/
	移动号段：134/135/136/137/138/139/
		147/
		150/151/152/157/158/159/
		178/
		182/183/184/187/188/
	 * @param mobiles
	 * @return
	 */
	private static boolean isMobile(String mobiles) {
		String regex = "^((13[0-9])|(14[5,7])|(15[^4,\\D])|(17[^4,\\D])|(18[0-9]))\\d{8}$";
		Pattern p = Pattern.compile(regex);
		
	    Matcher m = p.matcher(mobiles);
	    return m.matches();
	}


//编办业务  手机号

//代码



}
